<template>
  <transition name="el-fade-in">
    <div class="mask" v-show="$store.state.show_search" @click="$store.state.show_search = false">
      <div>
        <div @click.stop>
          <div class="input">
            <!-- <img src="@/assets/search.png" alt> -->
            <input
              type="text"
              v-model="search_value"
              @input="inSearch(search_value)"
              placeholder="请输入搜索内容"
            />
          </div>
          <div
            class="item"
            v-for="(item,index) in search_list"
            :key="index"
            @click="changePage(item.id)"
            v-show="search_value && search_list.length"
          >
            <img :src="item.image" alt />
            <span>{{item.name}}</span>
          </div>
          <div v-show="search_value && !search_list.length">暂无搜索结果</div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  data() {
    return {
      search_value: "",
      search_list: []
    };
  },
  methods: {
    inSearch(e) {
      let _this = this;
      if (e) {
        _this.$axios
          .get("site/searcher", {
            params: {
              name: e
            }
          })
          .then(res => {
            console.log(res);
            _this.search_list = res;
          });
      }
    },
    changePage(e) {
      this.$store.state.show_search = false;
      this.$router.push({
        path: "/teacher/index",
        query: {
          id: e
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.mask {
  width: 100%;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  left: 0;
  top: 0;
  z-index: 2002;
  > div {
    width: 8rem;
    height: 4rem;
    background: black;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 0.3rem 0 black;
    overflow: scroll;
    @media only screen and (max-width: 500px),
      only screen and (max-device-width: 500px) {
      width: calc(100% - 0.6rem);
      height: 60vh;
    }
    > div {
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding-top: 0.5rem;
      padding-bottom: 0.5rem;
      .input {
        width: 50%;
        height: 0.5rem;
        display: flex;
        flex-direction: row;
        align-items: center;
        input {
          width: 100%;
          height: 0.5rem;
          background: black;
          border-bottom: 2px @gold-color solid;
          font-size: @title-size;
          color: @gold-color;
          outline: none;
          text-align: center;
        }
      }
      > div {
        margin-top: 0.3rem;
      }
      .item {
        width: 50%;
        display: flex;
        flex-direction: row;
        align-items: center;
        background: @dark-color;
        cursor: pointer;
        @media only screen and (max-width: 500px),
          only screen and (max-device-width: 500px) {
          width: calc(100% - 0.6rem);
          height: 1.5rem;
        }
        > img {
          width: 1.4rem;
          height: 0.8rem;
          object-fit: cover;
          margin-right: 0.2rem;
          @media only screen and (max-width: 500px),
            only screen and (max-device-width: 500px) {
            width: 2rem;
            height: 100%;
          }
        }
      }
    }
  }
}
</style>
